<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Validation\Annotation\Scene;
use App\Service\SysFile as SysFileService;
use App\Request\SysFileRequest;
use Hyperf\HttpServer\Contract\RequestInterface;

#[AutoController]
class SysFileController extends AbstractController
{
    // 分组列表
    public function group_list(RequestInterface $request)
    {
        $type = $request->input('type', 'image');
        $result = (new SysFileService())->groupList(['type' => $type]);
        return $this->returnSuccess($result, '成功');
    }

    // 添加文件分组
    #[Scene(scene: 'addGroup', argument: 'request')]
    public function add_group(SysFileRequest $request)
    {
        // 获取通过验证的表单数据
        $params = $request->validated();
        $data = (new SysFileService())->addGroup($params);
        return $this->returnSuccess($data, '添加成功');
    }

    // 编辑文件分组
    #[Scene(scene: 'editGroup', argument: 'request')]
    public function edit_group(SysFileRequest $request)
    {
        // 获取通过验证的表单数据
        $params = $request->validated();
        $data = (new SysFileService())->editGroup($params['group_id'], $params);
        return $this->returnSuccess($data, '编辑成功');
    }

    // 删除文件分组
    public function delete_group()
    {
        $group_id = $this->request->input('group_id', 0);
        if (!$group_id)
            return $this->returnError('请求参数错误');
        $data = (new SysFileService())->deleteGroup($group_id);
        return $this->returnSuccess($data, '删除成功');
    }

    // 文件列表分页
    public function list()
    {
        $map = [];
        $group_id = $this->request->input('group_id', 0);
        if ($group_id) {
            $map[] = ['group_id', '=', $group_id];
        }

        $file_type = $this->request->input('file_type', 'image');
        if ($file_type) {
            $map[] = ['file_type', '=', $file_type];
        }

        $keyword = $this->request->input('keyword', '');

        if ($keyword) {
            $map[] = ['real_name', 'like', '%' . $keyword . '%'];
        }

        $per_page = $this->request->input('per_page', 10);

        $result = (new SysFileService())->list($map, (int)$per_page);
        return $this->returnSuccess($result, '成功');
    }
}
